package com.cpone.cloudweb.utils.bak;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Tool {
	/**
	 * 检查IP格式 zzb add
	 * @param ip
	 * @return
	 */
	public static boolean ipValid(String ip) {
		if(!(null==ip || "".equals(ip))){
			String regex0 = "(2[0-4]\\d)" + "|(25[0-5])";
			String regex1 = "1\\d{2}";
			String regex2 = "[1-9]\\d";
			String regex3 = "\\d";
			String regex = "(" + regex0 + ")|(" + regex1 + ")|(" + regex2 + ")|("
					+ regex3 + ")";
			regex = "(" + regex + ").(" + regex + ").(" + regex + ").(" + regex
					+ ")";
			Pattern pattern = Pattern.compile(regex);
			Matcher matcher = pattern.matcher(ip);
			return matcher.matches();
		}
		return false;
	}

	
	/**
	 * 判断字符串是否为空
	 * @param str 待判断字符串
	 * @return true false
	 */
	public static boolean isNull(String str) {
		boolean flag = false;
		if (str == null || "".equals(str) || "null".equals(str)) {
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 判断字符串是否能转化为double类型
	 * @param str 待判断字符串
	 * @return true false
	 */
	public static boolean isDouble(String str) {
		boolean flag = true;
		try {
			Double.parseDouble(str);
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
	
	/**
	 * String转换为Double类型
	 * @param str 待转换字符串
	 * @return double
	 */
	public static double stringToDouble(String str) {
		double d = 0;
		if (isDouble(str)) {
			d = Double.parseDouble(str);
		}
		return d;
	}
	
	/**
	 * 舍弃小数部分
	 * @param d double
	 * @return 整数
	 */
	public static String decimalOff(double d) {
		String str = String.valueOf(d);
		if (isNull(str)) {
			str = "";
		} else {
			str = str.substring(0, str.indexOf("."));
		}
		return str;
	}
	
	/**
	 * 隐藏号码
	 * @param number
	 * @return
	 */
	public static String hideNumber(String number){
		String temp = "";
		int length = number.length();
		if(length<=8){
			return number;
		}else{
			temp = number.substring(length-4,length);
			temp = number.substring(0,length-8)+"****"+temp;
			return temp;
		}
	}
}
